﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Noebe.Inject
{
    class NoebeInjectAssembler
    {
        private static volatile NoebeInjectAssembler instance;

        private static object syncRoot = new Object();

        public static NoebeInjectAssembler Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new NoebeInjectAssembler();
                    }
                }

                return instance;

            }
        }

        public INoebeManager GetManager(NoebeInjectConfiguration config)
        {
            Assembly assembly = Assembly.LoadFrom(config.FileName);

            if (assembly == null)
                return null;

            INoebeManager instance = (INoebeManager)assembly.CreateInstance(config.ClassName);

            if (!instance[config.TargetSingletonId].Initializes())
                throw new Exception("cannot initialize singleton noebe, singletonid = " + config.TargetSingletonId);

            return instance[config.TargetSingletonId];
        }

        private void test()
        {
            NoebeInjectConfiguration config = Configuration.GetConfiguration<NoebeInjectConfiguration>(StringHelper.global_configuartion_ioc_configname);

            GetManager(config);
        }
    }
}
